﻿using ModernUIApp1.ServiceReference2;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ModernUIApp1
{
    class WcfConnection
    {
        ServiceReference2.Service1Client myService = new Service1Client();

        public WcfConnection() { }

        public delegate bool AsyncMethodCaller(string username, string password);

        public bool validateUser(string username, string password)
        {
            if (myService == null)
            {
                myService = new ServiceReference2.Service1Client();
            }

            bool valid = false;

            valid = myService.validCredentials(username, password);

            return valid;
        }

        public int getUserId(String name)
        {
            if (myService == null)
            {
                myService = new ServiceReference2.Service1Client();
            }

            User user = myService.getUser(name);

            return user.id;
        }

        public List<Run> getRuns(int id)
        {
            if (myService == null)
            {
                myService = new ServiceReference2.Service1Client();
            }

            List<Run> runs = new List<Run>();

            foreach (Object run in myService.getRuns(id))
            {
                Type myType = run.GetType();
                IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

                Run myRun = new Run();
                object propValue = props[1].GetValue(run, null);
                myRun.endDate = DateTime.ParseExact(propValue.ToString(), "yyyy-MM-dd HH:mm:ss", null);
                propValue = props[3].GetValue(run, null);
                myRun.startDate = DateTime.ParseExact(propValue.ToString(), "yyyy-MM-dd HH:mm:ss", null);

                runs.Add(myRun);
            }

            return runs;
        }

        public List<Track> getTracks(int id)
        {
            if (myService == null)
            {
                myService = new ServiceReference2.Service1Client();
            }

            List<Track> tracks = new List<Track>();

            ModernUIApp1.ServiceReference2.Track[] objects = myService.getTracks(id);

            foreach (Object track in objects)
            {
                Type myType = track.GetType();
                IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());

                Track myTrack = new Track();
                object propValue = props[1].GetValue(track, null);
                myTrack.id = Convert.ToInt32(propValue.ToString());
                propValue = props[2].GetValue(track, null);
                myTrack.length = Convert.ToInt32(propValue.ToString());
                propValue = props[3].GetValue(track, null);
                myTrack.name = propValue.ToString();

                ModernUIApp1.ServiceReference2.Point[] pointsObjs = myService.getPoints(myTrack.id);

                foreach (Object point in pointsObjs)
                {
                    Type myPointType = point.GetType();
                    IList<PropertyInfo> pointProps = new List<PropertyInfo>(myPointType.GetProperties());

                    Point myPoint = new Point();
                    object ppVal = pointProps[3].GetValue(point, null);
                    myPoint.posX = float.Parse(ppVal.ToString(), CultureInfo.InvariantCulture);
                    ppVal = pointProps[4].GetValue(point, null);
                    myPoint.posY = float.Parse(ppVal.ToString(), CultureInfo.InvariantCulture);
                    ppVal = pointProps[2].GetValue(point, null);
                    myPoint.orderNum = Convert.ToInt32(ppVal.ToString());
                    myTrack.points.Add(myPoint);

                }
                
                tracks.Add(myTrack);
            }

            return tracks;
        }
    }
}
